jQuery(document).ready(function($) {
   // scorll to bottom, load more
   $('.exam_container').waypoint({
      offset: 'bottom-in-view',
      handler: function(direction) {
         if (direction === 'down') {
            var container = jQuery("#exam_container");
            var data = {};
            // how many questions right now
            var last_end = parseInt(jQuery('#last_end').text());
            // total count
            var total_num = parseInt(jQuery('#total_num').text());
            var sheetid = parseInt(jQuery('#sheetid').text());

            data.last_end = last_end;
            data.total_num = total_num;
            data.sheetid = sheetid;
            if (last_end >= total_num || total_num <= 5) {
               jQuery("#paperSubmitbtn").css('display','');
               return;
            };

            // Get 5 more question's html
            jQuery.ajax({
               type:"GET",
               url: loadmore_url,
               data:data,
               dataType: "html",
               beforeSend: function(xhr){
                  jQuery("#loadmorequstion").css('display','');
               }, success:function(data) { 
                  if (data) {
                     container.append("<div id='append_div_"+last_end+"' ></div>");
                     jQuery('#append_div_'+last_end).html(data);
                     jQuery('#last_end').text(parseInt(last_end)+5);
                     jQuery("#loadmorequstion").css('display','none');
                     if (last_end+5 >= total_num || total_num <= 5) {
                        jQuery("#paperSubmitbtn").css('display','');
                     };
                  }
                  // Enable the waypoint again to find the new container area bottom
                  $('.exam_container').waypoint('enable');
               }
            });
         };
      }
   });

}); 
